package com.yzjs.localhost.service.hrmScientific.impl;

import com.alibaba.fastjson.JSONObject;
import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dao.hrmScientific.HrmScientificMapper;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.hrmScientific.HrmScientificEntity;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.hrmScientific.HrmScientificService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;


@Service("hrmScientificService")
public class HrmScientificServiceImpl extends BaseServiceImpl<HrmScientificEntity, HrmScientificMapper> implements HrmScientificService {

    @Autowired
    AnalysisService analysisService;

    @Autowired
    HrmScientificService hrmScientificService;

    @Override
    public ComparisonResultDTO ScientificCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity before = dto.getBeforce();
        String beforeInfo = before.getInfo();
        Map<String, Object> beforeParse = JsonUtil.json2map(beforeInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo = after.getInfo();

        Map<String, Object> afterParseMap = JsonUtil.json2map(after.getInfo());
        Map<String, Object> differenceMap = new HashMap<>();
        ComparisonResultDTO resultDTO = null;

        if (before.getOperatorCode().contains("scientific:add")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 项目类型(系统菜单表),备注,项目代码,科研成果编号,成果简介,用户ID,主要完成人,是否删除(系统字典表),创建人,项目名称,获得时间
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse, "proType,kycgRemarks,proCode,kycgNum,gainMemo,kycgUser,majorPeo,deleteFlag,createBy,proName,gainDate");

        } else if (before.getOperatorCode().contains("scientific:edit")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 项目类型(系统菜单表) 备注 科研成果主键ID 项目代码 科研成果编号 成果简介 用户ID 主要完成人 是否删除(系统字典表) 创建人 更新人 项目名称 获得时间 创建时间
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse, "proType,kycgRemarks,kycgId,proCode,kycgNum,gainMemo,kycgUser,majorPeo,deleteFlag,createBy,updateBy,proName,gainDate,createDate");

        } else if (before.getOperatorCode().contains("scientific:remove")) {
            beforeParse.put("deleteFlag", "2");
            // 比对科研成果记录主键ID
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParseMap, "kycgIds,deleteFlag");
            Object kycgId = afterParseMap.get("kycgIds");
            if (kycgId == null) {
                resultDTO.setAbstractName("主键ID不存在");
                resultDTO.setWarningInfo("主键ID不存在");
                resultDTO.setMathFlag(false);
                return  resultDTO;
            }
            HrmScientificEntity scientificEntity = new HrmScientificEntity();
            scientificEntity.setKycgId(Long.valueOf((String) kycgId));
            scientificEntity.setDeleteFlag((String) afterParseMap.get("deleteFlag"));
            scientificEntity.setDataIdentification(after.getDataIdentification());
            this.insert(scientificEntity);
            return resultDTO;
        }

        // 记录表信息
        HrmScientificEntity hrmScientificEntity = JSONObject.parseObject(afterInfo, HrmScientificEntity.class);
        hrmScientificEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        hrmScientificEntity.setDataIdentification(after.getDataIdentification());
        this.insert(hrmScientificEntity);

        return resultDTO;
    }
}